/*
 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_STORE_H
#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_STORE_H

#include <vector>
#include <unordered_map>

#include "reminder_store_strategy.h"

#include "rdb_errno.h"
#include "rdb_helper.h"
#include "rdb_store_config.h"
#include "rdb_open_callback.h"

namespace OHOS::Notification {
class ReminderStore {
public:
    ReminderStore() = default;
    ~ReminderStore() = default;

public:
    int32_t Init();

    /**
     * @brief Delete reminder by reminder id, reminder type.
     */
    int32_t Delete(const int32_t reminderId, const int32_t type);
    /**
     * @brief Delete reminder by bundle name, user id, uid.
     */
    int32_t Delete(const std::string& bundleName, const int32_t userId, const int32_t uid);
    /**
     * @brief Delete reminder by user id.
     */
    int32_t Delete(const int32_t userId);
    /**
     * @brief Update if the reminder exists, otherwise insert.
     */
    int32_t UpdateOrInsert(const sptr<ReminderRequest>& reminder);
    /**
     * @brief Check reminder_id is in db.
     */
    bool IsReminderExist(const int32_t reminderId, const int32_t uid);
    /**
     * @brief Query reminder count.
     */
    int32_t QueryActiveReminderCount();
    /**
     * @brief Select all reminders.
     */
    std::vector<sptr<ReminderRequest>> GetAllValidReminders();
    /**
     * @brief Select reminders for the next X hours.
     */
    std::vector<sptr<ReminderRequest>> GetXHourReminders();

    int32_t GetMaxId();

public:
    static constexpr int32_t STATE_OK = 0;
    static constexpr int32_t STATE_FAIL = -1;

private:
    static void GetInt32Val(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
        const std::string& name, int32_t& value);

    /**
     * @brief Inits the data in database when system boot on or proxy process reboot on.
     *
     * 1. Deletes all the reminders which IS_EXPIRED is true.
     * 2. Sets all the value of STATE to ReminderRequest::REMINDER_STATUS_INACTIVE
     *
     * @return int32_t result code.
     */
    int32_t InitData();
    int32_t Delete(const std::string& baseCondition, const std::string& assoConditon);
    int32_t Insert(const sptr<ReminderRequest>& reminder);
    int32_t Update(const sptr<ReminderRequest>& reminder);

    bool IsReminderExist(const sptr<ReminderRequest>& reminder);
    std::vector<sptr<ReminderRequest>> GetReminders(const std::string& queryCondition);
    sptr<ReminderRequest> BuildReminder(const std::shared_ptr<NativeRdb::ResultSet>& result);

    std::shared_ptr<NativeRdb::ResultSet> Query(const std::string& tableName, const std::string& colums,
        const int32_t reminderId);
    std::shared_ptr<NativeRdb::ResultSet> Query(const std::string& queryCondition) const;

private:
    std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
    std::unordered_map<int32_t, std::shared_ptr<ReminderBaseStrategy>> strategy_;

private:
class ReminderStoreDataCallBack : public NativeRdb::RdbOpenCallback {
public:
    int32_t OnCreate(NativeRdb::RdbStore& store) override;
    int32_t OnUpgrade(NativeRdb::RdbStore& store, int32_t oldVersion, int32_t newVersion) override;
    int32_t OnDowngrade(NativeRdb::RdbStore& store, int32_t currentVersion, int32_t targetVersion) override;

private:
    int32_t CreateTable(NativeRdb::RdbStore& store);
    void AddRdbColum(NativeRdb::RdbStore& store, const std::string& tableName,
        const std::string& columnName, const std::string& columnType, const std::string& defValue);
};
};
}  // namespace OHOS::Notification
#endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_STORE_H